package com.ifreeee.practice.ywb;

import com.ifreeee.practice.bean.B;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 给定两个排序后的数组 A 和 B，编写一个方法，将两个数组合并 并排序。
 * 示例:
 * 输入:
 * A = [1,2,3]
 * B = [2,5,6]
 * 输出: [1,2,2,3,5,6]
 * 补全程序：
 */
class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        solution.merge(new int[]{1, 2, 3}, new int[]{2, 5, 6});
    }

    public void merge(int[] A, int[] B) {
        if (A == null) {
            System.out.println();
        }
        int[] ints = new int[A.length + B.length];
        int i = 0;
        int j = 0;
        int k = 0;
        while (i != A.length && j != B.length) {
            if (B[j] > A[i]) {
                ints[k++] = A[i++];
            } else {
                ints[k++] = B[j++];
            }
        }
        if (i == A.length) {
            while (j != B.length) {
                ints[k++] = B[j++];
            }
        } else if (j == B.length) {
            while (i != A.length) {
                ints[k++] = A[i++];
            }
        }
        System.out.println(Arrays.toString(ints));

    }

    public BigDecimal count(SkuService skuService, List<OrderItem> itemList, SkuCate skuCate) {
        BigDecimal bigDecimal = new BigDecimal("0");
        if (skuCate == null || skuCate.getId() == null) {
            return bigDecimal;
        }
        List<Sku> allSku = skuService.findAllSku();
        if (allSku == null) {
            return bigDecimal;
        }
        Map<Long, Long> idToCateId = new HashMap<>();
        for (Sku sku : allSku) {
            idToCateId.put(sku.getId(), sku.getSkuCateId());
        }
        if (itemList == null) {
            return bigDecimal;
        }
        for (OrderItem orderItem : itemList) {
            Long skuId = orderItem.getSkuId();
            Long cateId = idToCateId.get(skuId);
            if (cateId != null && cateId.equals(skuCate.getId())) {
                bigDecimal = bigDecimal.add(orderItem.getQty());
            }
        }
        return bigDecimal;
    }
}